#include "ErrorUtils.h"
#include "StringUtils.h"

namespace TestFramework
{
    namespace
    {
        const static TestString ErrorCodeText("%error-code%");
    }

    const TestString& GetErrorCodePattern()
    {
        return ErrorCodeText;
    }

    const char* FormatErrorText(const TestString& text, int code)
    {
        TestString result(text);
        const TestString::SizeType position = text.Find(ErrorCodeText);
        if (position != TestString::NPos)
        {
            const TestString codeText = ToString<TestString>(code);
            result.Replace(result.Begin() + position, result.Begin() + position + ErrorCodeText.GetLength(),
                           codeText.Begin(), codeText.End());
        }

        return StrCopy(result.Data());
    }

    const char* FormatErrorText(const char* errorDescription, int code)
    {
        return FormatErrorText(TestString(errorDescription), code);
    }

    void ReplaceSubstring(TestString& input, const TestString& pattern, const TestString& substitution)
    {
        TestString result(input);
        const TestString::SizeType position = result.Find(pattern);
        if (position != TestString::NPos)
        {
            result.Replace(result.Begin() + position, result.Begin() + position + pattern.GetLength(),
                           substitution.Begin(), substitution.End());
        }

        input = result;
    }
}
